package ec.atreasure.com.ec.login;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.util.Patterns;
import android.view.View;

import java.util.WeakHashMap;

import ec.atreasure.com.core.fragments.AtreasureFragment;
import ec.atreasure.com.core.net.rx.RxRestClient;
import ec.atreasure.com.ec.R;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import me.yokeyword.fragmentation.SupportActivity;

import static com.blankj.utilcode.util.BarUtils.getStatusBarHeight;

/**
 * Description :注册界面
 * Created by wangjin on 2019/6/27.
 * Job number：
 * Phone ：18301070822
 * Email： 120182051@qq.com
 * Person in charge : 汪渝栋
 * Leader：
 */
public class RegisterFragment extends AtreasureFragment {

    private TextInputEditText mName = null;
    private TextInputEditText mEmail = null;
    private TextInputEditText mPhone = null;
    private TextInputEditText mPassword = null;
    private TextInputEditText mRePassword = null;

    public IRegisterListener mRegisterListener = null;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Activity activity = (Activity) context;
        if (activity instanceof SupportActivity) {
            mRegisterListener = (IRegisterListener) activity;
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.register_fragment;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        mName = findViewById(R.id.edit_sign_up_name);
        mEmail = findViewById(R.id.edit_sign_up_email);
        mPhone = findViewById(R.id.edit_sign_up_phone);
        mPassword = findViewById(R.id.edit_sign_up_password);
        mRePassword = findViewById(R.id.edit_sign_up_re_password);
        findViewById(R.id.tb_sign_up).setPadding(0, getStatusBarHeight(), 0, 0);

        findViewById(R.id.btn_sign_up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                register();
            }
        });

        findViewById(R.id.tv_link_sign_in).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toLoginFragment();
            }
        });
    }


    private void register() {
        //TODO 注册后台交互逻辑
        if (checkForm()) {
            WeakHashMap<String, Object> weakHashMap = new WeakHashMap<>();
            RxRestClient.getRxRestClientBuilder()
                    .withUrl("")
                    .withParams(weakHashMap)
                    .setLoader(getContext())
                    .build()
                    .post()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<String>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(String s) {
                            //TODO 注册成功后逻辑操作
                            if (mRegisterListener != null)
                                mRegisterListener.onRegisterSuccess();
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    /**
     * 跳转到登录界面
     */
    private void toLoginFragment() {
        start(new LoginFragment(),SINGLETASK);
    }

    /**
     * 校验输入表单
     *
     * @return
     */
    private boolean checkForm() {
        final String name = mName.getText().toString();
        final String email = mEmail.getText().toString();
        final String phone = mPhone.getText().toString();
        final String password = mPassword.getText().toString();
        final String rePassword = mRePassword.getText().toString();

        boolean isPass = true;

        if (name.isEmpty()) {
            mName.setError("请输入姓名");
            isPass = false;
        } else {
            mName.setError(null);
        }

        if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            mEmail.setError("错误的邮箱格式");
            isPass = false;
        } else {
            mEmail.setError(null);
        }

        if (phone.isEmpty() || phone.length() != 11) {
            mPhone.setError("手机号码错误");
            isPass = false;
        } else {
            mPhone.setError(null);
        }

        if (password.isEmpty() || password.length() < 6) {
            mPassword.setError("请填写至少6位数密码");
            isPass = false;
        } else {
            mPassword.setError(null);
        }

        if (rePassword.isEmpty() || rePassword.length() < 6 || !(rePassword.equals(password))) {
            mRePassword.setError("两次密码输入不一致");
            isPass = false;
        } else {
            mRePassword.setError(null);
        }

        return isPass;
    }
}
